package com.dios.mirage.component;

import com.dios.mirage.navigation.Navigation;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class Accordion extends VerticalFieldManager {

	private AccordionHeader _header;
	private Manager _content;
	/*private LabelField _lblCollapse;
	private LabelField _lblTitle;*/
	private Bitmap _bitmapPlus;
	private Bitmap _bitmapMinus;
	
	public Accordion(String title, Manager content) {
		super(FOCUSABLE);

		_header = new AccordionHeader(title, true);
		_content = content;

		_bitmapPlus = Bitmap.getBitmapResource("img/plus.png");
		_bitmapMinus = Bitmap.getBitmapResource("img/minus.png");

		setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
		add(_header);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.rim.device.api.ui.Manager#paint(net.rim.device.api.ui.Graphics)
	 */
	protected void paint(Graphics graphics) {
		// TODO Auto-generated method stub
		super.paint(graphics);

	}

	private final class AccordionHeader extends Field {
		private String _title;
		private boolean _collapse;

		public AccordionHeader(String title, boolean collapse) {
			super(USE_ALL_WIDTH | FOCUSABLE);
			_title = title;
			_collapse = collapse;
		}/*

		public String getTitle() {
			return _title;
		}

		public void setTitle(String title) {
			_title = title;
		}*/

		public boolean isCollapse() {
			return _collapse;
		}

		public void setCollapse(boolean collapse) {
			_collapse = collapse;
			if (_collapse) {
				delete(_content);
			} else {
				add(_content);
			}
		}

		protected void layout(int width, int height) {
			// TODO Auto-generated method stub
			int fontHeight = getFont().getHeight();
			setExtent(width, fontHeight + 10);
		}

		protected void paint(Graphics graphics) {
			// TODO Auto-generated method stub

			graphics.setFont(Navigation.loadCustomFonts(getClass().getResourceAsStream(Navigation.fontPath), Font.BOLD, 30));
			graphics.drawLine(0, 0, getWidth(), 0);
			graphics.setColor(Color.WHITE);
			
			if(isCollapse()){
				if(_bitmapPlus!=null){
					graphics.drawBitmap(5, (getHeight()-_bitmapMinus.getHeight())/2, _bitmapPlus.getWidth(), _bitmapPlus.getHeight(), _bitmapPlus, 0, 0);
				}
			}else{
				if(_bitmapMinus!=null){
					graphics.drawBitmap(5, (getHeight()-_bitmapMinus.getHeight())/2, _bitmapMinus.getWidth(), _bitmapMinus.getHeight(), _bitmapMinus, 0, 0);
				}
			}
			graphics.drawText(_title, 30,
					5);
			graphics.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
		 */
		protected boolean navigationClick(int status, int time) {
			// TODO Auto-generated method stub
			setCollapse(!isCollapse());
			invalidate();
			return true;
		}

	}/*
	
	public void setCollapseIcon(Bitmap icon){
		_bitmapPlus = icon;
	}
	public void setExpandIcon(Bitmap icon){
		_bitmapMinus = icon;
	}*/
}
